package com.example.downloader.upload2;

public class Uploader<R> {
    final TaskExecutor<R> mTaskExecutor;
    private TaskQueue<R> mTaskQueue;

    public Uploader(TaskExecutor<R> taskExecutor) {
        this.mTaskExecutor = taskExecutor;
    }

    public void load(String path, Callback<R> callback) {
        mTaskQueue.releaseCallback(callback.getId());

        final Task<R> task = mTaskQueue.getTask(path);
        if (task != null && task.isActive()) {
            callback.attachTask(task);
            task.addCallback(callback);
            mTaskQueue.recordTaskCallback(callback);
        } else {
            final Task<R> N = new Task<>(mTaskExecutor, mTaskQueue, path);
            mTaskQueue.pushTask(path, N);
            callback.attachTask(N);
            mTaskQueue.recordTaskCallback(callback);
            N.execute();
        }

    }

}
